from rest_framework import serializers
from carts.models import CartRe
from goods.models import Goods


class GoodsCartSerializer(serializers.ModelSerializer):
    """商品显示"""
    class Meta:
        model = Goods
        fields = ["id", "name", "goods_front_image", "market_price", "shop_price",]

class CartReSerializer(serializers.ModelSerializer):
    """购物车商品推荐"""
    goods = GoodsCartSerializer()

    class Meta:
        model = CartRe
        fields = ["id", "show_status", "goods"]


from rest_framework import serializers
from goods.models import Goods, GoodsAttribute


class Goods_Serialier(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = ('name', "goods_front_image")


class CartSerializer(serializers.Serializer):
    """
    购物车参数
    """
    goodsattribute_id = serializers.IntegerField(min_value=1)
    count = serializers.IntegerField(min_value=1)
    selected = serializers.BooleanField(default=True)

    def validate(self, attrs):
        goods_id = attrs['goodsattribute_id']
        try:
            goods = GoodsAttribute.objects.get(id=goods_id)
        except GoodsAttribute.DoesNotExist:
            raise serializers.ValidationError('商品不存在')
        return attrs


class Cart_Put_Serializer(serializers.Serializer):  # put 方法修改购物车，接收goods_list参数
    """
    购物车参数
    """
    goodsattribute_list = serializers.ListField(child=serializers.DictField())

    def validate(self, attrs):
        goods_list = attrs['goodsattribute_list']
        for goods in goods_list:
            goods_id = goods.get('id')
            try:
                goods_in_law = GoodsAttribute.objects.get(id=goods_id)
            except Goods.DoesNotExist:
                raise serializers.ValidationError('商品不存在')
            storage_amount = goods['count']
        return attrs


class GoodsSerialier(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = ('name',)


class CartgoodsSerializer(serializers.ModelSerializer):
    """查询购物车序列化器"""
    goods = GoodsSerialier(read_only=True)
    count = serializers.IntegerField(min_value=1)
    selected = serializers.BooleanField(default=True)

    class Meta:
        model = GoodsAttribute
        fields = ('goods', 'id', 'size', 'price', 'color', 'image', 'count', 'selected',)


class CartDeleteSerializer(serializers.Serializer):
    goodsattribute_list = serializers.ListField(child=serializers.IntegerField(label='商品属性id', min_value=1))


class CartSelectAllSerializer(serializers.Serializer):
    """
    购物车全选
    """
    selected = serializers.BooleanField(label='全选')
